<template>
  <a-modal
    width='618px'
    title="上传交付物"
    centered
    :visible="modalVisible_upload"
    @ok="submitDeliver"
    @cancel="closeModal"
    okText="确认"
    cancelText="取消"
    >
    <common-upload class='local-upload' :oid='oid' @selectFileType='selectFileTypeHandler' @selectFileAddress='selectFileAddressHandler'
            @handleUpload='handleUploadHandler' @handleRemove='handleRemoveHandler' ref="uploadDeliver"></common-upload>
  </a-modal>
</template>
<script>
import commonUpload from '@/components/common/commonUpload'
import { uploadFromLocal } from "@api/planComponent";
export default {
  data() {
    return {
      selType: '',
      saveId: '',
      uploadFile: null ,
      modalVisible_upload: this.uploadModalVisible,//弹框显隐
    }
  },
  props: {
    uploadModalVisible: {
      type: Boolean,
      default: false,
    }
  },
  watch: {
    uploadModalVisible(val) {
      this.modalVisible_upload = val
    }
  },
  computed: {
    oid() {
      return sessionStorage.getItem('oid')
    }
  },
  components: {
    commonUpload
  },
  methods: {
   //选择文件类型
    selectFileTypeHandler(fileType) {
      this.selType = fileType
    },
    //文件存放位置
    selectFileAddressHandler(filePosition) {
      this.saveId = filePosition[filePosition.length - 1]
    },
    //文件上传
    handleUploadHandler(file) {
      this.uploadFile = file
    },
    //文件移除
    handleRemoveHandler(obj) {
      console.log('remove',obj)
    },
    //关闭弹框
    closeModal() {
      this.$emit('closeUploadModal')
    },
     //提交交付物
    submitDeliver() {
        //从本地上传
        let _this = this
        if (this.selType == '' || this.saveId == '') {
          this.$message.error('请选择文件类型和文件存放位置后上传!')
          return
        }
        if (this.uploadFile == null) {
          this.$message.error('请选择上传文件!')
          return
        }
        let params = new FormData()
        
        params.append('userId', JSON.parse(sessionStorage.getItem('userInfo')).loginName)
        params.append('docType', this.selType)
        params.append('folderOid', this.saveId)
        params.append('1_file', this.uploadFile)
        uploadFromLocal(params).then(res => {
          if (res.data.success != undefined && res.data.success) {
            // let result = res.data.data[0];
            // this.modalVisible_upload = false
            if(res.data.success) {
              this.$confirm({
              cancelText: "否",
              okText: "是",
              content: h => <div>文件已上传成功，是否启动文档签审？</div>,
              onOk() {
                /*_this.fileSingleData.tableArr = res
                _this.setFileSingle(_this.fileSingleData)*/
                let newSelectDeliverData = [], newSelectDeliverDataOnce = {}
                res.data.data.map((item) => {
                  newSelectDeliverDataOnce.oid = item['oid']
                  newSelectDeliverDataOnce.type = _this.selType
                  newSelectDeliverData.push(newSelectDeliverDataOnce)
                })
                let sessArr = {
                  tableArr: newSelectDeliverData
                }
                sessionStorage.setItem('fileSingleDataByStart', JSON.stringify(sessArr))
                _this.$router.push('/single/fileSingle/fileStart');
                _this.$emit("submitDeliver")
              },
              onCancel() {
                _this.$emit("submitDeliver")
              },
            })
            }
          }else {
            this.$message.error(res.data.message)
          }
        })
    },
    // 清除modal内容
    clearModal() {
      this.$refs.uploadDeliver.fileTypeData =[] 
      this.$refs.uploadDeliver.fileAddressData = []
      this.$refs.uploadDeliver.fileList = []
    }
  }
}
</script>
<style lang="less" scoped>
.width_410 {
  width: 410px;
}
</style>